#include"../../include/waiter.h"

#define TRUE  1
#define FALSE 0

using namespace std;

void GetGateWay(char gateway[])  
{  
    FILE *fp;  
    char buf[512];  
    char cmd[128]; 
    char *tmp;  

    strcpy(cmd, "ip route");  
    fp = popen(cmd, "r"); 
    if(NULL == fp)  
    {  
	perror("popen error");  
	return ;  
    }  
    while(fgets(buf, sizeof(buf), fp) != NULL)  
    {  
	tmp =buf;  
	while(*tmp && isspace(*tmp))  
	    ++ tmp;  
	if(strncmp(tmp, "default", strlen("default")) == 0)  
	    break;  
    }  
    sscanf(buf, "%*s%*s%s", gateway);         
    printf("本地网关:%s\n", gateway);  
    pclose(fp);  
}  



void Get_IP_addr (char ip_address[])
{
    struct ifaddrs * ifAddrStruct=NULL;
    void * tmpAddrPtr=NULL;

    char gateway[30];  
    getifaddrs(&ifAddrStruct);

 
    GetGateWay(gateway);
    
    while (ifAddrStruct!=NULL) 
    {
	if (ifAddrStruct->ifa_addr->sa_family==AF_INET)
	{   // check it is IP4
	    // is a valid IP4 Address
	    tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
	    char addressBuffer[INET_ADDRSTRLEN];
	    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
	    if(0==strncmp(gateway,addressBuffer,12))
	    {
		printf("网卡标识:%s\n本地IP地址:%s\n", ifAddrStruct->ifa_name, addressBuffer); 
		strcpy(ip_address,addressBuffer);
		break;
	    }
	}
	ifAddrStruct=ifAddrStruct->ifa_next;
    }

//    return 0;
}

WaiterClient::WaiterClient(){
    Msg.client=CurrentClient;
    OrderList = NULL;
    ChangeList= NULL;
}

WaiterClient::~WaiterClient(){
    delete OrderList;
    delete ChangeList;
}



void WaiterClient::ConnetServer(){
	//************TCP连接*****************//
	int ret;
	//建立套接字，确定要使用的协议家族
	socket_fd = socket(AF_INET,SOCK_STREAM,0);
	if(socket_fd == -1){
	    perror("socket");
	    return ;
	}

	//绑定地址和端口
	ServerSin.sin_family = AF_INET;
	ServerSin.sin_port = htons(ServerPort);
	//ip地址是服务器ip地址
	if(inet_pton(AF_INET,ServerLocalHost,&ServerSin.sin_addr.s_addr) <=0){
	    perror("inet_pton");
	    return ;
	}
	ret = connect(socket_fd,(struct sockaddr*)&ServerSin,sizeof(ServerSin));
	if(ret == -1){
	    perror("连接服务器失败");
	    return ;
	}else{
	    printf("成功连接服务器!\n");
	}

	//*************TCP连接结束*********************//
}

void WaiterClient::SendMessage(){
    Msg.client= CurrentClient;
    Msg.ContentSum=0;
    char buf[1024]={0};

    Content tmp;
    if(Msg.ConnetType==ConnetOrderTable||Msg.ConnetType==ConnetChangeTable){
	memcpy(buf,&Msg,HeaderSize);
	write(socket_fd,buf,1024);
    }else if(Msg.ConnetType==ConnetDeleteFood||Msg.ConnetType==ConnetAddNewFood){
	FoodList *p=ChangeList;
	char buf1[MaxPackSize-HeaderSize];
	while(p!=NULL){
	    ushort pos=Msg.ContentSum*ContentSize;
	    ++Msg.ContentSum;
	    tmp.FoodNum=p->FoodNum;
	    tmp.FoodSum=p->FoodSum;
	    strcpy(tmp.Remark,ChangeList->Remark);
	    memcpy(&buf1[pos],&tmp,ContentSize);
	    p=p->next;
	    if((Msg.ContentSum>=MaxContentSum)&&(p!=NULL)){
		Msg.PackIsEnd =NoEnd;
		memcpy(buf,&Msg,HeaderSize);
		memcpy(&buf[HeaderSize],buf1,MaxPackSize-HeaderSize);
		write(socket_fd,buf,1024);
		Msg.ContentSum=0;
		memset(buf1,0,MaxPackSize-HeaderSize);
	    }
	}
	Msg.PackIsEnd =IsEnd;
	memcpy(buf,&Msg,HeaderSize);
	memcpy(&buf[HeaderSize],buf1,MaxPackSize-HeaderSize);
	write(socket_fd,buf,1024);
	delete ChangeList;
    }else if(Msg.ConnetType==ConnetOrderFood||Msg.ConnetType==ConnetAddFood){
	FoodList *p=OrderList;
	char buf1[RWMaxSize-HeaderSize];
	while(p!=NULL){
	    ushort pos=Msg.ContentSum*ContentSize;
	    ++Msg.ContentSum;
	    tmp.FoodNum=p->FoodNum;
	    tmp.FoodSum=p->FoodSum;
	    strcpy(tmp.Remark,p->Remark);
	    memcpy(&buf1[pos],&tmp,ContentSize);
	    p=p->next;
	    if((Msg.ContentSum>=MaxContentSum)&&(p!=NULL)){
		Msg.PackIsEnd =NoEnd;
		memcpy(buf,&Msg,HeaderSize);
		memcpy(buf,buf1,MaxPackSize-HeaderSize);
		write(socket_fd,buf,1024);
		Msg.ContentSum=0;
		memset(buf1,0,MaxPackSize-HeaderSize);
	    }
	}
	Msg.PackIsEnd =IsEnd;
	memcpy(buf,&Msg,HeaderSize);
	memcpy(&buf[HeaderSize],buf1,MaxPackSize-HeaderSize);
	write(socket_fd,buf,1024);
	
    }else{
	Msg.PackIsEnd =IsEnd;
	memcpy(buf,&Msg,HeaderSize);
	write(socket_fd,buf,1024);
    }
}

void WaiterClient::ReceiveMessage(){
    char buf[RWMaxSize];
    read(socket_fd,buf,RWMaxSize);
    memcpy(&Msg,buf,HeaderSize);
    Content tmp={0,0,0};	
    if(Msg.ConnetType==ConnetGetCallWaiter||Msg.ConnetType==ConnetOkFood||Msg.ConnetType==ConnetGetTable||Msg.ConnetType==ConnetChangeTable||Msg.ConnetType==ConnetGetChangeTable||Msg.ConnetType==ConnetGetAccount){	
	int ContentSum=0;
	map<ushort,string>TmpResult;//桌号-称呼
	ResultTable=TmpResult;
	while(Msg.ContentSum--){
	    memcpy(&tmp,&buf[HeaderSize+ContentSize*ContentSum],ContentSize);
	    ++ContentSum;
	    ResultTable[tmp.FoodNum]=tmp.Remark;
	}
    }else if(Msg.ConnetType==ConnetAllTable||Msg.ConnetType==ConnetGetUsedTable){	
	int ContentSum=0;
	map<ushort,map<string,ushort>> TmpTable;//桌号-桌名-状态
	AllTable=TmpTable;
	while(Msg.ContentSum--){
	    memcpy(&tmp,&buf[HeaderSize+ContentSize*ContentSum],ContentSize);
	    ++ContentSum;
	    map<string,ushort>maptmp;
	    maptmp[tmp.Remark]=tmp.FoodSum;
	    AllTable[tmp.FoodNum]=maptmp;
	}
    }else if(Msg.ConnetType==ConnetGetFood){	
	int ContentSum=0;
	map<ushort,map<ushort,string>> TmpFood;//菜编号-价格-备注
	AllFood=TmpFood;
	while(Msg.ContentSum--){
	    memcpy(&tmp,&buf[HeaderSize+ContentSize*ContentSum],ContentSize);
	    ++ContentSum;
	    map<ushort,string>maptmp;
	    maptmp[tmp.FoodSum]=tmp.Remark;
	    AllFood[tmp.FoodNum]=maptmp;
	}
    }else if(Msg.ConnetType==ConnetGetOrderFood){	
	int ContentSum=0;
	FoodList *TmpList=new FoodList;
	TmpList->next=NULL;
	OrderList=TmpList;
	while(Msg.ContentSum--){
	    memcpy(&tmp,&buf[HeaderSize+ContentSize*ContentSum],ContentSize);
	    ++ContentSum;
	    FoodList *newTL=new FoodList;
	    newTL->next=NULL;
	    newTL->FoodNum=tmp.FoodNum;
	    newTL->FoodSum=tmp.FoodSum;
	    strcpy(newTL->Remark,tmp.Remark);
	    OrderList->next=newTL;
	    newTL->pre=OrderList;
	    OrderList=OrderList->next;
	}
	OrderList=TmpList->next;
    }else{	
    }
}


void WaiterClient::MainMenu(){
    cout<<"----------------------------------"<<endl;
    cout<<"1.开桌/换桌/处理换桌"<<endl;
    cout<<"2.点菜/加菜/删菜"<<endl;
    cout<<"3.呼叫信息"<<endl;
    cout<<"4.厨房可上菜信息"<<endl;
    cout<<"5.所有餐桌信息"<<endl;
    cout<<"6.结帐"<<endl;
    cout<<"----------------------------------"<<endl;
    cout<<">>";
    string Chose;
    cin>>Chose;
    while(Chose==""||Chose<"1"||Chose>"6"){
	cout<<"选择错误，重新选择"<<endl;
	cout<<">>";
	cin>>Chose;
    }
    if(Chose=="1"){
	OrderTable();
    }else if(Chose=="2"){
	OrderFood();
    }else if(Chose=="3"){
	DeelCallWaiter();
    }else if(Chose=="4"){
	CookStatus();
    }else if(Chose=="5"){
	GetAllTable();
    }else if(Chose=="6"){
	Account();
    }else{
	MainMenu();
    }

}
void WaiterClient::CookStatus(){
    string Chose;
    ushort EnterNum;
    while(1){
	Msg.ConnetType=ConnetOkFood;
	SendMessage();
	ReceiveMessage();
	cout<<"可上菜列表"<<endl;
	if(ResultTable.empty()){
	    cout<<"列表为空" <<endl;
	    MainMenu();
	    return;
	}
	ShowResultTable();
	cout<<"是否继续处理上菜状态？(y/n)"<<endl;
	cin>>Chose;
	if(Chose=="y"||Chose=="Y"){
	    cout<<"选择处理的餐桌编号"<<endl;
	    cin>>Chose;
	    EnterNum=atoi(Chose.c_str());
	    auto it=ResultTable.find(EnterNum);
	    if(it==ResultTable.end()){
		cout<<"选择房间编号错误"<<endl;
	    }else{
		Msg.ConnetType=ConnetFinishFood;
		Msg.TableNum=EnterNum;
		SendMessage();
		ReceiveMessage();
		break;
	    }

	}else{
	    MainMenu();
	    return;
	}
    }

    MainMenu();

}

void WaiterClient::GetAllTable(){
    Msg.ConnetType=ConnetAllTable;
    SendMessage();
    ReceiveMessage();
    ShowAllTable();
    Msg.ConnetType=ConnetGetFood;
    SendMessage();
    ReceiveMessage();
    if(AllFood.empty()){
	MainMenu();
	return;
    }
    cout<<"菜单详情"<<endl;
    for(auto it=AllTable.begin();it!=AllTable.end();it++){
	auto ii=it->second.begin()->second;
	if(ii!=0){
	    cout<<"桌号："<<it->first<<endl;
	    Msg.ConnetType=ConnetGetOrderFood;
	    Msg.TableNum=it->first;
	    SendMessage();
	    ReceiveMessage();
	    ShowOrderList();

	}
    }
    MainMenu();
}


void WaiterClient::OrderTable(){
    string Chose;
    ushort EnterNum;
    while(1){
	cout<<"请选择"<<endl;
	cout<<"1.开桌"<<endl;
	cout<<"2.换桌"<<endl;
	cout<<"3.处理换桌请求"<<endl;
	cout<<">>";
	cin>>Chose;
	if(Chose=="1"||Chose=="2"||Chose=="3"){
	    break;
	}else{
	    cout<<"没有这个选项"<<endl;
	}
    }
    if(Chose=="1"){

	Msg.ConnetType=ConnetGetTable;    
	map<ushort,string>newtable;
	ResultTable=newtable;
	SendMessage();
	ReceiveMessage();
	if(ResultTable.empty()){
	    cout<<"当前房间都满了"<<endl;
	    MainMenu();
	    return;
	}
	cout<<"当前空闲房间"<<endl;
	ShowResultTable();
	cout<<"您要订哪个房间呢"<<endl;
	while(1){
	    cout<<">>";
	    cin>>Chose;
	    EnterNum=atoi(Chose.c_str());
	    auto it=ResultTable.find(EnterNum);
	    if(it==ResultTable.end()){
		cout<<"不能预定该房间哦，请选择上述的房间编号"<<endl;
	    }else{
		CurrentTable=EnterNum;
		Msg.TableNum=EnterNum;
		break;
	    }
	}
	Msg.ConnetType=ConnetOrderTable;    
	SendMessage();
	ReceiveMessage();
    }else if(Chose=="2"){
	while(1){
	    Msg.ConnetType=ConnetGetUsedTable;
	    SendMessage();
	    ReceiveMessage();
	    cout<<"可以换桌的餐桌列表"<<endl;
	    ShowAllTable();
	    if(AllTable.empty()){
		cout<<"当前所有餐桌不可以调换哦"<<endl;
		MainMenu();
		return;
	    }
	    Msg.ConnetType=ConnetGetTable;
	    SendMessage();
	    ReceiveMessage();
	    cout<<"空闲餐桌列表"<<endl;
	    ShowResultTable();
	    if(ResultTable.empty()){
		cout<<"当前没有空闲餐桌可以调换哦"<<endl;
		MainMenu();
		return;
	    
	    }

	    cout<<"选择需要调换的餐桌"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    EnterNum=atoi(Chose.c_str());
	    if(AllTable.end()==AllTable.find(EnterNum)){
		cout<<"没有这个房间哦，重新选择吧"<<endl;
		continue;
	    }else{
		Msg.ConnetType=ConnetChangeTable;    
		cout<<"EnterNum="<<EnterNum<<endl;
		Msg.TableNum=EnterNum;
		CurrentTable=EnterNum;
		cout<<"374Msg.TableNum="<<Msg.TableNum<<endl;
		SendMessage();
		ReceiveMessage();
		Msg.ConnetType=ConnetGetTable;
		SendMessage();
		ReceiveMessage();
		cout<<"空闲餐桌列表"<<endl;
		ShowResultTable();
		cout<<"选择换到哪个餐桌"<<endl;
		cout<<">>";
		cin>>Chose;
		EnterNum=atoi(Chose.c_str());
		if(ResultTable.end()==ResultTable.find(EnterNum)){
		    cout<<"没有这个房间哦，重新选择吧"<<endl;
		    continue;
		}
		Msg.ConnetType=ConnetOrderTable;    
		Msg.TableNum=EnterNum;
		SendMessage();
		ReceiveMessage();
		Msg.TableNum=CurrentTable;
		Msg.ConnetType=ConnetDeelChangeTable;    
		SendMessage();
		ReceiveMessage();

		break;
	    }
	}

    }else if(Chose=="3"){
	while(1){
	    Msg.ConnetType=ConnetGetChangeTable;    
	    SendMessage();
	    ReceiveMessage();
	    cout<<"请求换桌列表"<<endl;
	    ShowResultTable();
	    if(ResultTable.empty()){
		cout<<"列表为空"<<endl;
		MainMenu();
		return;
	    }
	    cout<<"选择哪个餐桌"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    EnterNum=atoi(Chose.c_str());
	    if(ResultTable.end()==ResultTable.find(EnterNum)){
		cout<<"没有这个房间哦，重新选择吧"<<endl;
		continue;
	    }
	    Msg.TableNum=EnterNum;
	    Msg.ConnetType=ConnetDeelChangeTable;    
	    SendMessage();
	    ReceiveMessage();
	    cout<<"继续结帐？(y/n)"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    if(Chose=="y"||Chose=="Y"){
		continue;
	    }else{
		break;
	    }

	}
    }
    MainMenu();
}

void WaiterClient::OrderFood(){
    if(OrderList!=NULL)
	OrderList->next=NULL;
    OrderList=NULL;
    if(ChangeList!=NULL)
	ChangeList->next=NULL;
    ChangeList=NULL;
    Msg.ConnetType=ConnetGetUsedTable;
    SendMessage();
    ReceiveMessage();
    string Chose;
    ushort EnterNum;
    while(1){
	cout<<"房间列表"<<endl;
	ShowAllTable();
	cout<<"选择房间"<<endl;
	cout<<">>";
	cin>>Chose;
	EnterNum=atoi(Chose.c_str());
	if(AllTable.end()==AllTable.find(EnterNum)){
	    cout<<"没有这个房间哦，重新选择吧"<<endl;
	    continue;
	}else{
	    CurrentTable=EnterNum;
	    break;
	}
    }
    cout<<"选择操作类型"<<endl;
    cout<<"1.订菜"<<endl;
    cout<<"2.加菜"<<endl;
    cout<<"3.删菜"<<endl;
    cout<<"4.返回主菜单"<<endl;
    cout<<">>";
    cin>>Chose;
    if(Chose=="1"){
	Msg.ConnetType=ConnetGetOrderFood;
	Msg.TableNum=EnterNum;
	SendMessage();
	ReceiveMessage();
	if(OrderList!=NULL){
	    OrderList->next=NULL;
	    OrderList=NULL;
	    cout<<"已点菜，进行加菜操作"<<endl;
	    Chose="2";
	}
    }
    if(Chose=="1"){
	Msg.ConnetType=ConnetGetFood;
	SendMessage();
	ReceiveMessage();
	while(1){
	    ShowAllFood();
	    cout<<"----------------------------"<<endl;
	    cout<<"您的拟定菜单如下"<<endl;
	    ShowOrderList();
	    cout<<"输入菜编号(输入ok完成点菜)"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    FoodList *p=OrderList;
	    if(Chose=="ok"||Chose=="OK"){
		if(p){
		    Msg.ConnetType=ConnetOrderFood;
		    SendMessage();
		    ReceiveMessage();
		}else{
		    cout<<"未点任何菜哦"<<endl;
		}
		break;
	    }
	    EnterNum=atoi(Chose.c_str());
	    if(AllFood.end()==AllFood.find(EnterNum)){
		cout<<"没有这个菜哦，重新选择吧"<<endl;
		continue;
	    }
	    if(!p){
		OrderList=new FoodList;
		OrderList->next=NULL;
		p=OrderList;
		p->FoodNum=EnterNum;
		p->FoodSum=1;
	    }else{
		while(p){
		    if(p->FoodNum==EnterNum){
			p->FoodSum++;
			break;
		    }
		    if(p->next){
			p=p->next;
		    }else{
			FoodList *tmp=new FoodList;
			tmp->FoodNum=EnterNum;
			tmp->FoodSum=1;
			p->next=tmp;
			tmp->pre=p;
			p=p->next;
			break;
		    }
		}
	    }
	}
    
    }else if(Chose=="2"){
	Msg.ConnetType=ConnetGetFood;
	SendMessage();
	ReceiveMessage();
	Msg.ConnetType=ConnetGetOrderFood;
	Msg.TableNum=EnterNum;
	SendMessage();
	ReceiveMessage();
	while(1){
	    ShowAllFood();
	    cout<<"您的菜单如下"<<endl;
	    ShowOrderList();
	    cout<<"-----------------------------"<<endl;
	    cout<<"新添加菜单列表"<<endl;
	    ShowChangeList();
	    cout<<"输入加菜编号：(输入ok结束加菜)"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    if(Chose=="ok"||Chose=="OK"){
		if(ChangeList==NULL){
		    cout<<"本次未加菜"<<endl;
		}else{
		    Msg.ConnetType=ConnetAddFood;
		    SendMessage();
		    ReceiveMessage();
		    Msg.ConnetType=ConnetAddNewFood;
		    SendMessage();
		    ReceiveMessage();
		}
		break;
	    }
	    if(ChangeList==NULL){
		ChangeList=new FoodList;
		ChangeList->next=NULL;
	    }
	    FoodList *p=ChangeList;
	    FoodList *p1=OrderList;
	    EnterNum=atoi(Chose.c_str());
	    if(AllFood.end()==AllFood.find(EnterNum)){
		cout<<"没有这个菜哦，重新选择吧"<<endl;
		continue;
	    }
	    while(p1){
		if(p1->FoodNum==EnterNum){
		    p1->FoodSum++;
		    break;
		}
		if(!p1->next){
		    while(p){
			if(p->FoodNum==EnterNum){
			    p->FoodSum++;
			    break;
			}
			if(!p->next){
			    FoodList *tmp=new FoodList;
			    tmp->next=NULL;
			    tmp->FoodNum=EnterNum;
			    tmp->FoodSum=1;
			    p->next=tmp;
			    tmp->pre=p;
			    p=p->next;
			    break;
			}
			p=p->next;
		    }
		    break;
		}
		p1=p1->next;
	    }
	}
    }else if(Chose=="3"){
	Msg.ConnetType=ConnetGetOrderFood;
	Msg.TableNum=EnterNum;
	SendMessage();
	ReceiveMessage();
	if(OrderList==NULL){
	    cout<<"未订菜"<<endl;
	    MainMenu();
	    return;
	}
	while(1){
	    cout<<"您已订的菜单如下"<<endl;
	    ShowOrderList();
	    cout<<"-----------------------------"<<endl;
	    cout<<"删除菜单列表"<<endl;
	    ShowChangeList();
	    cout<<"输入删除的菜编号：(输入ok结束删除)"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    if(Chose=="ok"||Chose=="OK"){
		if(ChangeList==NULL){
		    cout<<"本次未删菜"<<endl;
		}else{
		    ChangeList=ChangeList->next;
		    Msg.ConnetType=ConnetDeleteFood;
		    SendMessage();
		    ReceiveMessage();
		}
		break;
	    }
	    if(ChangeList==NULL){
		ChangeList=new FoodList;
		ChangeList->next=NULL;
	    }
	    FoodList *p=ChangeList;
	    FoodList *p1=OrderList;
	    EnterNum=atoi(Chose.c_str());
	    while(p1){
		if(p1->FoodNum==EnterNum){
		    p1->FoodSum--;
		    if(p1->FoodSum==0){
			if(p1==OrderList){
			    if(p1->next!=NULL){
				OrderList=OrderList->next;
			    }else{
				OrderList=NULL;
			    }

			}else{
			    if(p1->next!=NULL){
				p1->pre->next=p1->next;
				p1->next->pre=p1->pre;
			    }else{
				p1->pre->next=NULL;
			    }
			}
			delete p1;
		    }
		    break;
		}
		if(!p1->next){
		    cout<<"您没有点该菜哦"<<endl;
		    p=NULL;
		    break;
		}
		p1=p1->next;
	    }
	    while(p){
		if(p->FoodNum==EnterNum){
		    p->FoodSum++;
		    break;
		}
		if(!p->next){
		    FoodList *tmp=new FoodList;
		    tmp->next=NULL;
		    tmp->FoodNum=EnterNum;
		    tmp->FoodSum=1;
		    p->next=tmp;
		    tmp->pre=p;
		    p=p->next;
		    break;

		}
		p=p->next;
	    }
	}

    }else{
	MainMenu();
	return;
    }

    MainMenu();
}


void WaiterClient::Warming(){
    Msg.ConnetType=ConnetGetCallWaiter;
    SendMessage();
    ReceiveMessage();
    if(!ResultTable.empty()){
	cout<<"以下餐桌呼叫服务员"<<endl;
	ShowResultTable();
    }
    Msg.ConnetType=ConnetGetAccount;
    SendMessage();
    ReceiveMessage();
    if(!ResultTable.empty()){
	cout<<"以下餐桌要结帐"<<endl;
	ShowResultTable();
    }
    Msg.ConnetType=ConnetGetChangeTable;    
    SendMessage();
    ReceiveMessage();
    if(!ResultTable.empty()){
	cout<<"以下餐桌要换桌"<<endl;
	ShowResultTable();
    }

}



void WaiterClient::DeelCallWaiter(){
    string Chose;
    ushort EnterNum;
    while(1){
	Msg.ConnetType=ConnetGetCallWaiter;
	SendMessage();
	ReceiveMessage();
	if(ResultTable.empty()){
	    MainMenu();
	    return;
	}
	cout<<"以下餐桌呼叫服务员"<<endl;
	ShowResultTable();
	cout<<"选择"<<endl;
	cout<<">>";
	cin>>Chose;
	EnterNum=atoi(Chose.c_str());
	if(ResultTable.end()==ResultTable.find(EnterNum)){
	    cout<<"没有这个房间哦，重新选择吧"<<endl;
	    continue;
	}
	Msg.ConnetType=ConnetCallWaiter;
	Msg.TableNum=EnterNum;
	SendMessage();
	ReceiveMessage();
	cout<<"继续处理？(y/n)"<<endl;
	cout<<">>";
	cin>>Chose;
	if(Chose=="y"||Chose=="Y"){
	    continue;
	}else{
	    break;
	}
    }
    MainMenu();

}


void WaiterClient::Account(){
    Msg.ConnetType=ConnetGetAccount;
    SendMessage();
    ReceiveMessage();
    cout<<"待结帐列表"<<endl;
    ShowResultTable();
    ushort EnterNum;
    string Chose;
    if(ResultTable.empty()){
	cout<<"列表为空"<<endl;
	cout<<"-------------------"<<endl;
	cout<<"选择：1.手动结帐  2.返回"<<endl;
	cout<<">>";
	cin>>Chose;
	if(Chose=="1"){
	    Chose="2";
	}else{
	    Chose="3";
	}
    }else{
	cout<<"-------------------"<<endl;
	cout<<"选择：1.处理结帐请求 "<<endl;
	cout<<"      2.手动结帐"<<endl;
	cout<<"      3.返回"<<endl;
	cout<<">>";
	cin>>Chose;
    
    }
    if(Chose=="1"){
	while(1){
	    Msg.ConnetType=ConnetGetAccount;
	    SendMessage();
	    ReceiveMessage();
	    cout<<"待结帐列表"<<endl;
	    ShowResultTable();
	    if(ResultTable.empty()){
		cout<<"列表为空"<<endl;
		MainMenu();
		return;

	    }
	    cout<<"选择结帐餐桌"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    EnterNum=atoi(Chose.c_str());
	    if(ResultTable.end()==ResultTable.find(EnterNum)){
		cout<<"没有这个房间哦，重新选择吧"<<endl;
		continue;
	    }
	    Msg.ConnetType=ConnetAccount;
	    Msg.TableNum=EnterNum;
	    SendMessage();
	    ReceiveMessage();
	    cout<<"继续结帐？(y/n)"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    if(Chose=="y"||Chose=="Y"){
		continue;
	    }else{
		break;
	    }
	}
    }else if(Chose=="2"){
	while(1){
	    Msg.ConnetType=ConnetGetUsedTable;
	    SendMessage();
	    ReceiveMessage();
	    ShowAllTable();
	    if(AllTable.empty()){
		cout<<"没有餐桌可以结帐"<<endl;
		MainMenu();
		return;
	    }
	    cout<<"选择结帐餐桌"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    EnterNum=atoi(Chose.c_str());
	    if(AllTable.end()==AllTable.find(EnterNum)){
		cout<<"没有这个房间哦，重新选择吧"<<endl;
		continue;
	    }
	    Msg.ConnetType=ConnetAccount;
	    Msg.TableNum=EnterNum;
	    SendMessage();
	    ReceiveMessage();
	    cout<<"继续结帐？(y/n)"<<endl;
	    cout<<">>";
	    cin>>Chose;
	    if(Chose=="y"||Chose=="Y"){
		continue;
	    }else{
		break;
	    }
	}
    }else{
	MainMenu();
	return;
    }


    MainMenu();
}

void WaiterClient::ShowAllFood(){
    cout<<"本店菜单"<<endl;
    if(AllFood.empty())return;
    for(auto it=AllFood.begin();it!=AllFood.end();it++){
	auto ii=it->second.begin();
	cout<<"编号："<<it->first<<" "<<ii->second<<" ￥"<<ii->first<<endl;
    }
}
void WaiterClient::ShowResultTable(){
    if(ResultTable.empty())return;
    for(auto it=ResultTable.begin();it!=ResultTable.end();it++){
   cout<<"房间："<<it->first<<" "<<it->second<<endl;
    }

}

void WaiterClient::ShowOrderList(){
    FoodList *p=OrderList;
    if(AllFood.empty())return;
    while(p){
	cout<<p->FoodNum<<" "<<(AllFood.find(p->FoodNum)->second).begin()->second<<" 份数："<<p->FoodSum<<endl;
	p=p->next;
    }
}

void WaiterClient::ShowChangeList(){
    FoodList *p =ChangeList;
    if(p){
	p=p->next;
    }
    while(p){
	cout<<p->FoodNum<<" "<<p->Remark<<" 份数："<<p->FoodSum<<endl;
	p=p->next;
    }
}
void WaiterClient::ShowAllTable(){
    cout<<"所有餐台列表"<<endl;
    if(!AllTable.empty()){
	for(auto it=AllTable.begin();it!=AllTable.end();it++){

	    cout<<"桌号："<<it->first<<" ";
	    cout<<"桌名："<<((it->second).begin())->first<<" ";
	    cout<<"状态：";
	    switch((it->second).begin()->second){
		case 0:cout<<"空闲"<<endl;break;
		case 1:cout<<"服务中"<<endl;break;
		case 2:cout<<"呼叫服务员"<<endl;break;
		case 3:cout<<"待结帐"<<endl;break;
		case 4:cout<<"待上菜"<<endl;break;
		case 5:cout<<"待换房"<<endl;break;
		default:cout<<"未知"<<endl;
	    }
	}
    }
}



int main()
{
    WaiterClient waiter;
    waiter.ConnetServer();
    waiter.MainMenu();
    return 0;
}
